// 抽象 获取编号服务
var orderNo = orderNo || {};
orderNo.url = sysInfo.basePath + '/sc/general/allotScOrder/getScProductOrderNo.do';
orderNo.scProOrderNoArr = []; // 请求的编号
orderNo.scProOrderNoUseddArr = []; // 使用了的的编号
orderNo.useCacheScProOrderNo = true;  // 使用js缓存的 编号

// 从缓存里取出
orderNo.getFromJsArr = function (callback) {
    for (var i in this.scProOrderNoArr) {
        var inUse = false, no = this.scProOrderNoArr[i].no;
        for (var j in this.scProOrderNoUseddArr) {
            if (no == this.scProOrderNoUseddArr[j].no) {
                inUse = true;
                break;
            }
        }
        if (!inUse) {
            this.scProOrderNoUseddArr.push(this.scProOrderNoArr[i]);
            callback(this.scProOrderNoArr[i]);
            break;
        }
    }
}

// 从后台取出
orderNo.getFromJavaWeb = function (callback) {
    DGG.Layer.loading.start();
    $.ajax({
        url: orderNo.url + '?t=' + (new Date().getTime()),
        data: {},
        type: "POST",
        dataType: "json",
        success: function (obj) {
            DGG.Layer.loading.done();
            if (obj.code != 0) {
                DGG.Layer.message(obj.msg, {icon: '2'}, function () {
                });
            } else {
                orderNo.scProOrderNoArr.push(obj.data);
                if (callback) {
                    orderNo.scProOrderNoUseddArr.push(obj.data);
                    callback(obj.data);
                }
            }
        }
    });
}

// 获取Id和编号
orderNo.getIdAndNo = function (callback) {
    if (this.useCacheScProOrderNo && this.scProOrderNoArr.length > 0 && this.scProOrderNoUseddArr.length < this.scProOrderNoArr.length) {
        this.getFromJsArr(callback);
        return;
    }
    this.getFromJavaWeb(callback);
}

// 获取编号
orderNo.getNo = function (callback) {
    if (callback) {
        this.getIdAndNo(function (data) {
            callback(data.no);
        })
    }
}

// 取消使用
orderNo.cancelUseNo = function (no) {
    var index = -1;
    for (var i in this.scProOrderNoUseddArr) {
        if (no == this.scProOrderNoUseddArr[i].no) {
            index = i;
            break;
        }
    }
    // 编号置为 未使用
    this.scProOrderNoUseddArr.splice(index, 1);
}
